package cn.likole.oj.service;

import cn.likole.oj.bean.Achievement;
import cn.likole.oj.bean.Solution;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;

/**
 * Created by likole on 8/1/18.
 */
@IocBean
public class AchievementService {

    @Inject
    private Dao dao;

    /**
     * 在答案不正确时触发
     * @param solution
     */
    public void onError(Solution solution){
        if(solution.getProblemId()==0) return;//测试运行
        if(solution.getResult()<4) return;//尚未评测完
        int pid=solution.getProblemId();
        int errorNum= dao.func(Solution.class,"count","id", Cnd.where("problemId","=",pid).and("result","!=","4"));
        if(errorNum>=20){//顽强拼搏
            Achievement achievement=new Achievement();
            achievement.setAchievementId(0);
            achievement.setUserId(solution.getUserId());
            achievement.setAchieveTime(new Date());
            dao.insert(achievement);
        }
        if(errorNum>=21){//顽强拼搏升级版
            Achievement achievement=new Achievement();
            achievement.setAchievementId(1);
            achievement.setUserId(solution.getUserId());
            achievement.setAchieveTime(new Date());
            dao.insert(achievement);
        }
    }
}
